package com.bsj.power.pls.mqtt.listener;

import com.bsj.power.pls.netty.CachedChannelMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.core.server.MqttServer;
import net.dreamlu.iot.mqtt.core.server.MqttServerCreator;
import net.dreamlu.iot.mqtt.core.server.event.IMqttConnectStatusListener;
import net.dreamlu.mica.redis.cache.MicaRedisCache;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;
import org.tio.core.ChannelContext;

/**
 * @author yinhao
 * @version 1.0
 * @description 监听Mqtt连接状态
 * @date 2023/11/7
 */
@Slf4j
public class MqttConnectStatusListener implements IMqttConnectStatusListener{

    /**
     * 客户端上线
     *
     * @param channelContext
     * @param clientId
     * @param username
     */
    @Override
    public void online(ChannelContext channelContext, String clientId, String username) {
        log.info("MqttClientOnline clientId: {}  username: {}", clientId, username);
        // 上线的时候不添加双向map，在设备上行数据的时候才添加，应为上线时候只有clientId，没有deviceId，没有对应关系

    }

    /**
     * 客户端离线
     *
     * @param channelContext
     * @param clientId
     * @param username
     * @param reason
     */
    @Override
    public void offline(ChannelContext channelContext, String clientId, String username, String reason) {
        log.info("MqttClientOffline clientId: {}  username: {}  reason: {}", clientId, username, reason);
        // 离线的时候，删除双向map中的数据
        CachedChannelMap.delClientId(clientId);
    }


}
